<?php
class User_Model_UsersCrafts extends Zend_Db_Table_Abstract
{
	protected $_name = 'users_crafts';
	protected $_sequence = 'users_crafts_id_seq';
	protected $_referenceMap  = array(
	        'User' => array(
	            'columns'           => array('id'),
	            'refTableClass'     => 'Users',
	            'refColumns'        => array('id_user')
	        ),
	        'Craft' => array(
	            'columns'           => array('id'),
	            'refTableClass'     => 'Crafts',
	            'refColumns'        => array('id_craft')
	        ),
	        'Sector' => array(
	            'columns'           => array('id'),
	            'refTableClass'     => 'Sectors',
	            'refColumns'        => array('id_sector')
			)
	    );
	
	public function addUserCraft($id_craft,$id_sector,$date_created)
	{		
		$usersDbTable = new User_Model_Users();

		$data = array(
			'id_user'		=> new Zend_Db_Expr("CURRVAL('".$usersDbTable->getSequence()."')"),
			'id_craft'		=> $id_craft,
			'id_sector'		=> $id_sector,
			'date_created' 	=> $date_created,
		);
		$var = $this->insert($data);
		
	}
	
	public function updateUserCraft($id_user,$id_craft,$id_sector)
	{
		$data = array(
			'id_craft'		=> $id_craft,
			'id_sector'		=> $id_sector,
		);
		$this->update($data, 'id_user = '. $id_user);
	}
	
	public function getUserCraftByUser($id_user)
	{
		$id = (int)$id;
		$row = $this->fetchRow('id_user = ' . $id_user);
		
		if (!$row) {
			return false;
		} else {	
			return $row->toArray();
		}
	}
	
}